package swind.web.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import swind.web.dao.ITestDao;
import swind.web.service.IManageTestService;
import swind.web.service.ITeacherService;
import swind.web.vo.ErrorMessageResult;
import swind.web.vo.RemindVo;
import swind.web.vo.SimpleUserVo;
import swind.web.vo.SubjectVo;
import swind.web.vo.TeaGetScoreVo;
import swind.web.vo.TeaGetStudentVo;
import swind.web.vo.TestTypeAndSubjectVo;
import swind.web.vo.TestTypeVo;
import swind.web.vo.TestUpdateVo;
import swind.web.vo.TestVo;

@Controller
public class TeacherController {
	
	@Resource(name="teacherServiceImp")
	ITeacherService teacherServiceImp;
	
	@Resource(name="manageTestServiceImp")
	IManageTestService manageTestServiceImp;
	
	@RequestMapping(value="tea/subshow")
	public String show(Model model){
		model.addAttribute("subjects",teacherServiceImp.getAllSubject());
		
		return "mainPages/teacher/subjectTables";
	}
	
	@ResponseBody
	@RequestMapping(value="tea/add",method=RequestMethod.POST)
	public ErrorMessageResult add(@Valid SubjectVo addSubject,BindingResult br){
		ErrorMessageResult errorMessageResult=new ErrorMessageResult();
		if(br.hasErrors()){
			
			errorMessageResult.setSuccess(false);
			List<FieldError> errorList=br.getFieldErrors();
			for(FieldError fieldError:errorList){
				errorMessageResult.getErrorMessage().put(fieldError.getField(), fieldError.getDefaultMessage());
				
			}
			return errorMessageResult;
		}
		
		errorMessageResult.setSuccess(true);
		System.out.println("add subject:"+addSubject.getName());
		System.out.println(addSubject.isStatus());
		teacherServiceImp.addSubject(addSubject);
		return errorMessageResult;
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/getupdate")
	public SubjectVo getupdate(String subjectId){
		System.out.println("getupdate:"+subjectId);
		return teacherServiceImp.getSubjectById(Integer.parseInt(subjectId));
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/update",method=RequestMethod.POST)
	public ErrorMessageResult update(@Valid SubjectVo updateSubject,BindingResult br){
		ErrorMessageResult errorMessageResult=new ErrorMessageResult();
		if(br.hasErrors()){
			
			errorMessageResult.setSuccess(false);
			List<FieldError> errorList=br.getFieldErrors();
			for(FieldError fieldError:errorList){
				errorMessageResult.getErrorMessage().put(fieldError.getField(), fieldError.getDefaultMessage());
				
			}
			return errorMessageResult;
		}
		
		errorMessageResult.setSuccess(true);
		System.out.println("update subject:"+updateSubject.getName());
		teacherServiceImp.updateSubject(updateSubject);
		return errorMessageResult;
	}
	
	
	
	@RequestMapping(value="tea/delete")
	public String delete(String subId){
		System.out.println("delete subject:"+subId);
		teacherServiceImp.deleteSubject(Integer.parseInt(subId));
		return "redirect:/tea/subshow";
	}
		
	
	@ResponseBody
	@RequestMapping(value="tea/userSubjectRelation")
	public List<SimpleUserVo> userSubjectRelation(String subjectId){
		System.out.println("userSubjectRelation:"+subjectId);
		return teacherServiceImp.findSimpleUserBySubjectId(Integer.parseInt(subjectId));
	}
	
	@ResponseBody
	@RequestMapping(value="tea/setSubjectAjax")
	public void setSubjectAjax(String subjectId,String[] checkedList,String[] notCheckedList){
		System.out.println("setSubjectAjax");
		System.out.println("subjectId="+subjectId);
		teacherServiceImp.setSubject(Integer.parseInt(subjectId), checkedList, notCheckedList);
		
		
	}
	
	
	@RequestMapping(value="tea/queryScore")
	public String queryScore(){
		
		
		return "mainPages/teacher/queryScore";
	}
	
	@ResponseBody
	@RequestMapping(value="tea/quertRemind")
	public RemindVo quertRemind(String remindValue){
		System.out.println("quertRemind:"+remindValue);
		
		return teacherServiceImp.getQuertRemind(remindValue);
	}
	
	
	@RequestMapping(value="tea/listScore")
	public String listScore(String inSelect,Model model){
		String[] queryData = inSelect.split(",");
		
		try {
			List<TeaGetScoreVo> list = teacherServiceImp.queryScoreByData(queryData);
			model.addAttribute("scoreList",list);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("stop");
		return "mainPages/teacher/queryScore";
	}
	
	
	@RequestMapping(value="tea/listStudent")
	public String listStudent(Model model){
		System.out.println("modstu");
		model.addAttribute("stuVoList", teacherServiceImp.getAllStudent());
		return "mainPages/teacher/modifyStudent";
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/addStudent",method=RequestMethod.POST)
	public ErrorMessageResult addStudent(@Valid TeaGetStudentVo getStudentVo,BindingResult br){
		ErrorMessageResult errorMessageResult=new ErrorMessageResult();
		if(br.hasErrors()){
			
			errorMessageResult.setSuccess(false);
			List<FieldError> errorList=br.getFieldErrors();
			for(FieldError fieldError:errorList){
				errorMessageResult.getErrorMessage().put(fieldError.getField(), fieldError.getDefaultMessage());
				
			}
			return errorMessageResult;
		}
		
		errorMessageResult.setSuccess(true);
		
		
		try {
			teacherServiceImp.addStudent(getStudentVo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			errorMessageResult.setSuccess(false);
			errorMessageResult.getErrorMessage().put("userId", "用户已存在,请重新输入userId");
		}
		return errorMessageResult;
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/getupdatestu")
	public TeaGetStudentVo getupdatestu(String sutdentId){
		System.out.println("getupdatestu:"+sutdentId);
		return teacherServiceImp.getTeaStuVoById(sutdentId);
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/updatestu",method=RequestMethod.POST)
	public ErrorMessageResult updatestu(@Valid TeaGetStudentVo getStudentVo,BindingResult br){
		ErrorMessageResult errorMessageResult=new ErrorMessageResult();
		if(br.hasErrors()){
			
			errorMessageResult.setSuccess(false);
			List<FieldError> errorList=br.getFieldErrors();
			for(FieldError fieldError:errorList){
				errorMessageResult.getErrorMessage().put(fieldError.getField(), fieldError.getDefaultMessage());
				
			}
			return errorMessageResult;
		}
		
		errorMessageResult.setSuccess(true);

		teacherServiceImp.updateStudent(getStudentVo);
		return errorMessageResult;
	}

	@RequestMapping(value="tea/deleteStu")
	public String deleteStu(String stuId){
		System.out.println("delete subject:"+stuId);
		teacherServiceImp.deleteStudent(stuId);
		return "redirect:/tea/listStudent";
	}
	
	@ResponseBody	
	@RequestMapping(value="tea/fileUpload")
	public ErrorMessageResult fileUpload(@RequestParam("fileSelect") MultipartFile[] fileSelect) throws IOException{
		ErrorMessageResult errorMessageResult=new ErrorMessageResult();
		errorMessageResult.setSuccess(false);
	
		teacherServiceImp.fileReceive(fileSelect, errorMessageResult);

		return errorMessageResult;
	}
	
	
	
	@RequestMapping(value="tea/toManageTest")
	public String toManageTest(Model model){
		
		model.addAttribute("tests",manageTestServiceImp.findAll());
		
		return "mainPages/teacher/manageTest";
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/getDataToAdd",method=RequestMethod.POST)
	public TestTypeAndSubjectVo getDataToAdd(){
		

		TestTypeAndSubjectVo testTypeAndSubjectVo = manageTestServiceImp.getAllTypeAndSubject();
		
		
		return testTypeAndSubjectVo;
	}
	
	@ResponseBody
	@RequestMapping(value="tea/getScoreAjax",method=RequestMethod.POST)
	public TestTypeVo getScoreAjax(String typeId){
		
		return manageTestServiceImp.getScoreType(Integer.valueOf(typeId));
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/addTest",method=RequestMethod.POST)
	public ErrorMessageResult addTest(@Valid TestVo testVo,BindingResult br){
		ErrorMessageResult errorMessageResult=new ErrorMessageResult();
		if(br.hasErrors()){
			
			errorMessageResult.setSuccess(false);
			List<FieldError> errorList=br.getFieldErrors();
			for(FieldError fieldError:errorList){
				errorMessageResult.getErrorMessage().put(fieldError.getField(), fieldError.getDefaultMessage());
				
			}
			return errorMessageResult;
		}
		
		errorMessageResult.setSuccess(true);

		manageTestServiceImp.addTest(testVo);
		
		
		return errorMessageResult;
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/getDataToFix",method=RequestMethod.POST)
	public TestUpdateVo getDataToFix(String testId){
		

		TestUpdateVo testVo = manageTestServiceImp.getTestById(Integer.valueOf(testId));
		
		
		return testVo;
	}
	
	
	@ResponseBody
	@RequestMapping(value="tea/updateTest",method=RequestMethod.POST)
	public ErrorMessageResult updateTest(@Valid TestVo testVo,BindingResult br){
		ErrorMessageResult errorMessageResult=new ErrorMessageResult();
		if(br.hasErrors()){
			
			errorMessageResult.setSuccess(false);
			List<FieldError> errorList=br.getFieldErrors();
			for(FieldError fieldError:errorList){
				errorMessageResult.getErrorMessage().put(fieldError.getField(), fieldError.getDefaultMessage());
				
			}
			return errorMessageResult;
		}
		
		errorMessageResult.setSuccess(true);

		manageTestServiceImp.updateTest(testVo);
		
		
		return errorMessageResult;
	}
	
	@RequestMapping(value="tea/deleteTest")
	public String deleteTest(String stuId){
		manageTestServiceImp.deleteTest(Integer.valueOf(stuId));
		
		return "redirect:/tea/toManageTest";
	}
	
}
